#include "cust_apply_withdraw_dialog.h"
#include "ui_cust_apply_withdraw_dialog.h"
#include "QSqlDatabase"
#include "QSqlQuery"
#include "QDebug"
#include "QDateTime"
#include "QMessageBox"
#include "withdraw_info.h"
#include "QDateTime"
#include "bts_mgmt.h"

cust_apply_withdraw_dialog::cust_apply_withdraw_dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::cust_apply_withdraw_dialog)
{
    ui->setupUi(this);
    this->setWindowTitle("申请取款");
}

cust_apply_withdraw_dialog::~cust_apply_withdraw_dialog()
{
    delete ui;
}

void cust_apply_withdraw_dialog::on_confirm_clicked()
{
    //先查询是否可进行取款
    QSqlDatabase sqlite_dep = QSqlDatabase::database("deposit");
    QSqlQuery query_dep(sqlite_dep);
    query_dep.prepare("SELECT islost, savingsamount, custpassword FROM DEPOSIT WHERE custtelnumber = ? ");
    query_dep.addBindValue(ui->user_card_id->text());
    if(query_dep.exec()) query_dep.next();
    else qDebug() << "查询失败";
    if(query_dep.value(0).toString() == "未挂失")
    {
        if(query_dep.value(1).toString() == "未进行储蓄")
        {
            //处理存款中无金额情况(未存款)
            QMessageBox box;
            box.setWindowTitle("warning");
            box.setText("您未进行过存款或已取出存款");
            box.setIcon(QMessageBox::Information);
            box.exec();
            return;
        }
        else
        {
            if(query_dep.value(2).toString() == ui->user_password->text())
            {
                //进行取款操作
                //先判断有没有待审核的取款项目，若有则结束取款(防止多次申请)
                QSqlDatabase sqlite_wd = QSqlDatabase::database("withdraw");
                QSqlQuery query_wd(sqlite_wd);
                query_wd.prepare("SELECT reviewstatus FROM WITHDRAW WHERE custtelnumber = ?" );
                query_wd.addBindValue(ui->user_card_id->text());
                query_wd.exec();
                query_wd.next();
                if(query_wd.value(0) == "待审核")
                {
                    QMessageBox box;
                    box.setWindowTitle("warning");
                    box.setText("您已经申请过取款了");
                    box.setIcon(QMessageBox::Warning);
                    box.exec();
                    return;
                }
                //先删除上一条取款内容
                query_wd.prepare("DELETE FROM WITHDRAW WHERE custtelnumber = ?" );
                query_wd.addBindValue(ui->user_card_id->text());
                query_wd.exec();
                //先获取基本信息
                withdraw_info withdraw;
                query_dep.prepare("SELECT * FROM DEPOSIT WHERE custtelnumber = ? ");
                query_dep.addBindValue(ui->user_card_id->text());
                if(query_dep.exec()) query_dep.next();
                else qDebug() << "查询失败";
                withdraw.setCustTelnumber(query_dep.value(1).toString());withdraw.setCustName(query_dep.value(2).toString());
                withdraw.setCustPassword(query_dep.value(3).toString());withdraw.setCustAddress(query_dep.value(4).toString());
                withdraw.setSavingsType(query_dep.value(5).toString());withdraw.setSavingsAmount(query_dep.value(6).toString());
                withdraw.setSavingsTime(query_dep.value(7).toString());withdraw.setIsLost(query_dep.value(8).toString());
                withdraw.setLostTime(query_dep.value(9).toString());withdraw.setClerkName(query_dep.value(10).toString());
                withdraw.setClerkEmployeeid(query_dep.value(11).toString());withdraw.setReviewStatus("待审核");
                QString wdtime = QDateTime::currentDateTime().toString();
                withdraw.setWithdrawTime(wdtime);
                //设置额外的取款相关信息
                double money = withdraw.calInterest();
                QString interest = QString::number(money,'f',2);
                withdraw.setInterest(interest);
                double result_amount = money + query_dep.value(6).toInt();
                QString withdraw_amount = QString::number(result_amount,'f',2);
                withdraw.setWithdrawAmount(withdraw_amount);
                withdraw.insertToDB();
                //BTS_Mgmt bm;
                BTS_Mgmt::insertToTransactionDB(query_dep.value(1).toString(),"取款",withdraw_amount,wdtime);
                return;
            }
            else
            {
                QMessageBox box;
                box.setWindowTitle("warning");
                box.setText("密码错误，请重新输入");
                box.setIcon(QMessageBox::Warning);
                box.exec();
                return;
            }
        }
    }
    else//处于挂失状态
    {
        QMessageBox box;
        box.setWindowTitle("warning");
        box.setText("您的账户处于挂失状态，请联系营业员处理");
        box.setIcon(QMessageBox::Warning);
        box.exec();
        return;
    }
}
